package cn.com.hongfu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.com.hongfu.model.User;
import cn.com.hongfu.service.UserService;
import cn.com.hongfu.utils.Utils;
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	public UserService userService ;
	/**
	 * 返回添加人员视图
	 * @return
	 */
	@RequestMapping("/addUI")
	public String addUI(){
		return "/user/userAdd";
	}
	/**
	 * 对添加人员进行处理
	 * @param user
	 * @param modelMap
	 * @return 服务器端定向到展示所有用户页面
	 */
	@RequestMapping("/add")
	public String addUser(User user,ModelMap modelMap){
		user.setPassword(Utils.encrypt(user.getPassword()));
		userService.save(user);
		String url = "redirect:/user/list.html" ;
		return url;
	}
	/**
	 * 返回所有人员页面的视图
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/list")
	public String showUsers(ModelMap modelMap){
		modelMap.addAttribute("users", userService.all());
		return "/user/userList";
	}
	/**
	 * 通过人员ID删除人员
	 * @param modelMap
	 * @param userId
	 * @return
	 */
	@RequestMapping("/delUser/{userId}")
	public ModelAndView showUsers(ModelMap modelMap,@PathVariable String userId){
		return new ModelAndView("redirect:/user/list.html");
	}
	/**
	 * 返回编辑页面视图
	 * @param modelMap
	 * @param userId
	 * @return
	 */
	@RequestMapping("/editUserUI/{userId}")
	public String editUserUI(ModelMap modelMap,@PathVariable String userId){
		return "/user/editUser";
	}
	/**
	 * 对编辑人员进行处理
	 * @param user
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/editUser")
	public String editUserUI(User user,ModelMap modelMap){
		modelMap.addAttribute("flag", "1");
		return "/user/editUser";
	}
}
